package com.yykj.hospital.server.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.enums.HospitalAdminType;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.client.dto.HospitalAdminDTO;
import com.yykj.hospital.client.form.HospitalAdminListForm;
import com.yykj.hospital.client.dto.HospitalAdminListDTO;
import com.yykj.hospital.server.dto.AdminPasswordDTO;
import com.yykj.hospital.server.dto.HospitalAdminMenuDTO;
import com.yykj.hospital.server.dto.HospitalLoginInfo;
import com.yykj.hospital.client.form.HospitalAdminEditForm;
import com.yykj.hospital.server.form.HospitalAdminLoginForm;
import com.yykj.hospital.server.service.HospitalAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;

@RestController
@RequestMapping("/api/backstage/hospital/admin")
@SuppressWarnings("unchecked")
@Api(tags = "医院账号相关")
public class HospitalAdminController {

    @PostMapping("/login")
    @ApiOperation("登录接口")
    public ResultVO<HospitalLoginInfo> login(@RequestBody HospitalAdminLoginForm hospitalAdminLoginForm) {
        ResultVO<HospitalLoginInfo> success = ResultVO.success(hospitalAdminService.login(hospitalAdminLoginForm));
        success.setMsg("");
        return success;
    }

    @GetMapping("/login/info/{token}")
    @ApiOperation("获取登录信息")
    public ResultVO<HospitalLoginInfo> loginInfo(@PathVariable String token) {
        return ResultVO.success(hospitalAdminService.loginInfo(token));
    }

    @GetMapping("/menu/list/{adminId}")
    @ApiOperation("获取用户的菜单")
    public  ResultVO<HospitalAdminMenuDTO>  getHospitalAdminMenu(@PathVariable Integer adminId){
        return ResultVO.success(hospitalAdminService.getHospitalAdminMenu(adminId));
    }


    @GetMapping("/logout")
    @ApiOperation("登出接口")
    public ResultVO logout() {
        hospitalAdminService.logout();
        return ResultVO.success();
    }

    @PostMapping("/add")
    @ApiOperation("管理员--添加")
    @ApiOperationSupport(ignoreParameters = {"id","type"})
    public ResultVO<HospitalAdminDTO> addAdmin(@RequestBody HospitalAdminEditForm form) {
        form.setType(HospitalAdminType.user);
        return ResultVO.success(hospitalAdminService.editAdmin(form));
    }

    @PutMapping("/edit")
    @ApiOperation("管理员--修改")
    @ApiOperationSupport(ignoreParameters = {"type"})
    public ResultVO<HospitalAdminDTO> editAdmin(@RequestBody HospitalAdminEditForm form) {
        form.setType(HospitalAdminType.user);
        return ResultVO.success(hospitalAdminService.editAdmin(form));
    }

    @DeleteMapping("/delete/{adminId}")
    @ApiOperation("管理员--删除")
    public ResultVO deleteAdmin(@PathVariable Integer adminId) {
        hospitalAdminService.deleteAdmin(adminId);
        return ResultVO.success();
    }

    @GetMapping("/list")
    @ApiOperation("管理员--列表")
    public ResultVO<PageData<HospitalAdminListDTO>> listAdmin(@RequestParam(name = "hospitalId") Integer hospitalId,
                                                              @RequestParam(required = false, name = "username") String username,
                                                              @RequestParam(required = false, name = "keyword") String keyword,
                                                              @RequestParam(name = "currentPage",defaultValue = "1") Integer currentPage,
                                                              @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize) {
        HospitalAdminListForm form = HospitalAdminListForm.builder()
                .hospitalId(hospitalId)
                .username(username)
                .keyword(keyword)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();

        return ResultVO.success(hospitalAdminService.adminList(form));
    }

    @ApiOperation("管理员--修改密码")
    @PostMapping("/change/admin/password")
    public ResultVO changeAdminPassword(@RequestBody AdminPasswordDTO adminPasswordDTO){
        hospitalAdminService.changeAdminPassword(adminPasswordDTO);
        return ResultVO.success();
    }


    private HospitalAdminService hospitalAdminService;

    @Autowired
    public void setHospitalAdminService(HospitalAdminService hospitalAdminService) {
        this.hospitalAdminService = hospitalAdminService;
    }
}
